package sg4c.frontend;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.util.HashMap;
import sg4c.frontend.command.GraphicsCommand;

public class GraphicsContext {

    private final Graphics2D graphics;
    private Color strokeColor = Color.black;
    private Color fillColor = Color.white;
    private float strokeWeight = 1.0f;
    private HashMap<Integer, Font> fontMap = new HashMap<Integer, Font>();

    public GraphicsContext(Graphics2D g) {
        this.graphics = g;
    }

    public boolean noStroke() {
        return strokeColor == null || strokeWeight == 0;
    }

    public boolean noFill() {
        return fillColor == null;
    }

    public Graphics2D getGraphics() {
        return graphics;
    }

    public void execute(GraphicsCommand gc) {
        gc.execute(this);
    }

    public void setStrokeColor(Color color) {
        strokeColor = color;
    }

    public void setFillColor(Color color) {
        fillColor = color;
    }

    public Color getStrokeColor() {
        return strokeColor;
    }

    public Color getFillColor() {
        return fillColor;
    }

    public void loadFillColor() {
        graphics.setPaint(fillColor);
    }

    public void loadStrokeColor() {
        graphics.setPaint(strokeColor);
    }

    public void setStrokeWeight(float weight) {
        strokeWeight = weight;
        graphics.setStroke(new BasicStroke(weight));
    }

    public float getStrokeWeight(float weight) {
        return strokeWeight;
    }

    public void setFont(int fontId) {
        Font font = fontMap.get(fontId);
        graphics.setFont(font);
    }

    public int createFont(int id, String fontname, int style, int pointSize) {
        Font font = new Font(fontname, style, pointSize);
        fontMap.put(id, font);
        return id;
    }

    public int createFont(int id, String fontname, int pointSize) {
        return createFont(id, fontname, 0, pointSize);
    }

    public FontMetrics getFontMetrics(int fontId) {
        return graphics.getFontMetrics(fontMap.get(fontId));
    }
}

